<script setup>
import { ref, nextTick, onMounted } from "vue";
// 引入 echarts 核心模块，核心模块提供了 echarts 使用必须要的接口。
import * as echarts from "echarts/core";
// 引入折线图图表，图表后缀都为 Chart
import { GaugeChart } from "echarts/charts";
// 引入提示框，标题，直角坐标系，数据集，内置数据转换器组件，组件后缀都为 Component
import {
  TitleComponent,
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
} from "echarts/components";
// 标签自动布局、全局过渡动画等特性
import { UniversalTransition } from "echarts/features";
// 引入 Canvas 渲染器，注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from "echarts/renderers";
import waterContent from "@/assets/waterContent/content.json";

// 注册必须的组件
echarts.use([
  TitleComponent,
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  GaugeChart,
  UniversalTransition,
  CanvasRenderer,
]);

// 接收数据
const props = defineProps({
  typeOf: {
    type: String,
    default: "",
  },
  contentKey: {
    type: String,
    default: "",
  },
  contentName: {
    type: String,
    default: "",
  },
  contentValue: {
    type: Number,
    default: 0.0,
  },
});

let max = 0;
let standardL = 0;
let standardH = 0;
const initData = () => {
  max = waterContent[props.typeOf][props.contentKey]["max"];
  standardL = waterContent[props.typeOf][props.contentKey]["standardL"];
  standardH = waterContent[props.typeOf][props.contentKey]["standardH"];
};
initData();

// 初始化图表
const optionForm = {
  series: [
    {
      type: "gauge",
      startAngle: 180,
      endAngle: 0,
      center: ["50%", "85%"],
      radius: "85%",
      splitNumber: 8,
      min: 0,
      max: 0,
      axisLine: {
        lineStyle: {
          width: 3,
          color: [
            [1, "#FDDD60"],
            [1, "#7CFFB2"],
            [1, "#FF6E76"],
          ],
        },
      },
      pointer: {
        icon: "path://M12.8,0.7l12,40.1H0.7L12.8,0.7z",
        length: "32%",
        width: "8%",
        offsetCenter: [0, "-60%"],
        itemStyle: {
          color: "auto",
        },
      },
      axisTick: {
        length: 12,
        lineStyle: {
          color: "auto",
          width: 0,
        },
      },
      splitLine: {
        length: 20,
        lineStyle: {
          color: "auto",
          width: 0,
        },
      },
      axisLabel: {
        color: "#fff",
        fontSize: 8,
        distance: -40,
        rotate: "tangential",
        formatter: function (value) {
          if (
            value === max / 8 ||
            value === (3 * max) / 8 ||
            value === (5 * max) / 8 ||
            value === (7 * max) / 8
          )
            return value;
        },
      },
      title: {
        offsetCenter: [0, "-10%"],
        color: "#fff",
        fontSize: "8%",
      },
      textStyle: {
        color: "#fff",
        fontSize: "8%",
      },
      detail: {
        fontSize: "14%",
        offsetCenter: [0, "-35%"],
        valueAnimation: true,
        // formatter: function (value) {
        //   return Math.round(value * 1) + "";
        // },
        color: "inherit",
      },
      data: [
        {
          value: 0.0,
          name: "",
        },
      ],
    },
  ],
};
const option = ref({ ...optionForm });

let containerRef = ref(null);
const initChart = () => {
  const myChart = echarts.init(containerRef.value);

  option.value.series[0].max = max;
  option.value.series[0].data[0].name = props.contentName;
  option.value.series[0].data[0].value = props.contentValue.toFixed(2);
  option.value.series[0].axisLine.lineStyle.color[0][0] = standardL / max;
  option.value.series[0].axisLine.lineStyle.color[1][0] = standardH / max;

  myChart.setOption(option.value);
};

onMounted(() => {
  nextTick(() => {
    initChart();
  });
});
</script>

<template>
  <div ref="containerRef" id="container"></div>
</template>

<style scoped>
#container {
  height: 100%;
  width: 100%;
}
</style>